package com.kazarus.examplx00006;



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.widget.Button;
import android.view.View;  
import android.view.View.OnClickListener;


public class examplx00006 extends Activity 
{
	private WebView webView;
	private Button  Btnx_View;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        webView = (WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        //webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl("http://www.cnblogs.com/hibraincol/");
        
        Btnx_View = (Button)findViewById(R.id.button1);
        Btnx_View.setOnClickListener(new OnBtnxViewClickListener());
        
   
    }
    
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// TODO Auto-generated method stub
//		menu.add(0, 1, 1,R.string.exit);
//		menu.add(0, 2, 2,R.string.quit);
//		menu.add(0, 3, 3,R.string.showsource);
//		
//		return super.onCreateOptionsMenu(menu);
//	}
	
    final class MyWebViewClient extends WebViewClient{  
        public boolean shouldOverrideUrlLoading(WebView view, String url) {   
            view.loadUrl(url);   
            return true;   
        }  
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d("WebView","onPageStarted");
            super.onPageStarted(view, url, favicon);
        }    
        public void onPageFinished(WebView view, String url) {
            Log.d("WebView","onPageFinished ");
            view.loadUrl("javascript:window.local_obj.showSource('<head>'+" +
                    "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
            super.onPageFinished(view, url);
        }
    }
     
    final class InJavaScriptLocalObj {
        public void showSource(String html) {
            Log.d("HTML", html);
        }
    }
    
    class OnBtnxViewClickListener implements OnClickListener
    {
    	@Override
    	public void onClick (View v)
    	{
    		
    		webView.loadUrl("javascript:window.local_obj.showSource('<head>'+" +
            "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    		Toast.makeText(examplx00006.this, "cao", Toast.LENGTH_SHORT).show();
    	}
    }
    
//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		
//		
//		//switch (item.getOrder()) {
//		switch (item.getItemId()) {
//		case 1:
//			Toast.makeText(this, "you clike 1 item.", Toast.LENGTH_LONG).show();
//			break;
//		case 2:
//			Toast.makeText(this, "you clike 2 item.", Toast.LENGTH_LONG).show();
//		    break;	
//		case 3:
//			this.finish();
//			break;
//			
//		default:break;
//		}
//		
//		
//		return false;
//		
//	}    
}